<?php
// +----------------------------------------------------------------------
//            -------------------------
//           /   / ----------------\  \
//          /   /             \  \
//         /   /              /  /
//        /   /    /-------------- /  /
//       /   /    /-------------------\  \
//      /   /                   \  \
//     /   /                     \  \
//    /   /                      /  /
//   /   /      /----------------------- /  /
//  /-----/      /---------------------------/
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://baimifan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: Sage 2016/4/19  14:05
// +----------------------------------------------------------------------
namespace Wechat\Controller;
use Think\Controller;

//活动
Class ActivityController extends BaseController {
    function index() {
        //活动列表
        $map=[
//            'activity_start'=>['lt',date('Y-m-d H:i:s')],
//            'activity_end'=>['gt',date('Y-m-d H:i:s')]
            'apply_start'=>['lt',date('Y-m-d H:i:s')],  //报名中的活动
            'apply_end'=>['gt',date('Y-m-d H:i:s')]
        ];
        $Activity = M("Activity");
        $d = $Activity->where($map)->select();
        $user_id = session("user_id");
        //已报名的活动
        $v_order = M("View_activity_order");
        $o = $v_order->where(['user_id'=>$user_id,'status'=>1])->field("activity_id,is_refund")->select();  //查出本用户已经报名成功的数据
        $actid = array();//存放报名了的Id
        $refund = array();//存放退款状态 活动Id=>状态
        foreach ($o as $k => $v) {
            array_push($actid, $v['activity_id']);          //存放报名了的Id
            $refund[$v['activity_id']] = $v['is_refund'];   //存放退款状态 活动Id=>状态
        }
        //已收藏的活动
        $fav = M("Favorite_activity");
        $f = $fav->where("user_id=$user_id")->field("activity_id")->select();
        $favid = array();
        foreach ($f as $k => $v) {
            array_push($favid, $v['activity_id']);
        }

        foreach ($d as $k => $v) {
            if (in_array($d[$k]["activity_id"], $actid)) {
                $d[$k]["is_apply"] = 1; //已报名
                if($refund[$d[$k]["activity_id"]] == 1){    //判断是否退款
                    $d[$k]["is_refund"] = 1;
                }else{
                    $d[$k]["is_refund"] = 0;
                }
            } else {
                $d[$k]["is_apply"] = 0; //未报名
            }
            if (in_array($d[$k]["activity_id"], $favid)) {
                $d[$k]["is_fav"] = 1; //已收藏
            } else {
                $d[$k]["is_fav"] = 0; //未收藏
            }
        }
        $this->assign("d", $d);
        $this->display();
    }

    function detail() {
        $activity_id = I("get.id");
        //活动详情
        $Activity = M("Activity");
        $d = $Activity->where("activity_id=$activity_id")->find();
        //已报名活动列表
        $user_id = session("user_id");
        $v_order = M("View_activity_order");
        $d['is_apply'] = 0;     //未报名-初始
//        $order = $v_order->where(['activity_id'=>$activity_id,'user_id'=>$user_id,'status'=>1,'is_refund'=>'0'])->find();
        $order = $v_order->where(['activity_id'=>$activity_id,'user_id'=>$user_id,'status'=>1,])->find();
        if(!empty($order)){     //有报名订单
            $d['is_apply'] = 1;     //已报名
            if($order['refund_id']!=0){ //有退款申请未成功
                $d['refund_status'] = M("Refund")->where(["refund_id"=>$order['refund_id']])->getField("status");
            }
//            var_dump($d);exit;
        }
        //是否加入收藏
        $fav_act = M("favorite_activity");
        $map['activity_id'] = $activity_id;
        $map['user_id'] = $user_id;
        $d['is_fav'] = $fav_act->where($map)->count(); //是否收藏
        //
        $this->assign("d", $d);
        $this->display();
    }

    //取消收藏-ajax调用
    function cancel_fav() {
        $activity_id = I("get.id");
        $user_id = session("user_id");
        if (!is_numeric($activity_id)) {
            echo 0;
            return;
        }
        $fav_act = M("favorite_activity");
        $map['activity_id'] = $activity_id;
        $map['user_id'] = $user_id;
        $count = $fav_act->where($map)->count();
        if ($count == 0) {
            echo 0;
            return;
        }
        $fav_act->startTrans();
        $fav_act->where($map)->delete();
        $count1 = $fav_act->where($map)->count();
        if ($count1 == 0) {
            $fav_act->commit();
            echo 1;
        } else {
            $fav_act->rollback();
            echo 0;
        }
    }

    //加入收藏-ajax调用
    function join_fav() {
        $activity_id = I("get.id");
        $user_id = session("user_id");
        if (!is_numeric($activity_id)) {
            echo 0;
            return;
        }
        $fav_act = M("favorite_activity");
        $map['activity_id'] = $activity_id;
        $map['user_id'] = $user_id;
        $count = $fav_act->where($map)->count();
        if ($count > 0) {
            echo 0;
            return;
        }
        $count1 = $fav_act->data($map)->add();
        if ($count1) {
            echo 1;
        } else {
            echo 0;
        }
    }

    //申请退款-活动-添加退款记录
    function refund(){
        $Acta=M('View_activity_apply');
        $Acta->startTrans();

        $activity_id = I("get.id");
        $user_id = session("user_id");
        $v_order = M("View_activity_order");
        $data = $v_order->where(['activity_id'=>$activity_id,'user_id'=>$user_id,'status'=>1,'is_refund'=>'0'])->find(); //是否报名
        if(empty($data)){
            $Acta->rollback();
            $j=[
                'status'=>'error',
                'data'=>'订单不存在'
            ];
            echo json_encode($j,JSON_UNESCAPED_UNICODE);
            exit;
        }
        //活动报名
        $Acta=M('View_activity_apply');
        $Acta->startTrans();
        $da=$Acta->where(['apply_id'=>$data['apply_id'],'is_refund'=>'0'])->find();
        if(empty($da)){
            $Acta->rollback();
            $j=[
                'status'=>'error',
                'data'=>'订单不存在'
            ];
            echo json_encode($j,JSON_UNESCAPED_UNICODE);
            exit;
        }
        $end_time=date('Y-m-d H:i:s',strtotime(date('Y-m-d',strtotime($da['apply_end']))));
        if(time() > strtotime($end_time)+24*3600){
            $Acta->rollback();
            $j=[
                'status'=>'error',
                'data'=>'无法退货款，只可在报名结束前一天退款'
            ];
            echo json_encode($j,JSON_UNESCAPED_UNICODE);
            exit;
        }
        $dp=M('Pay')->where(['pay_id'=>$da['pay_id']])->find();
        //生成退货款订单
        $map_refund=[
            'user_id'=>$dp['user_id'],
            'status'=>0,    //状态：下单
            'type'=>3,      //活动
            'way'=>'wechat',  //线下
            'summary'=>'活动退款,订单号:'.$data['apply_id'].',支付流水号:'.$dp['pay_id'],
            'money'=>$dp['money_real']-$dp['money_zero'],   //金钱 = 实收 - 找零
            'money_real'=>0   //实收
        ];
        $refund_id=M('Refund')->data($map_refund)->add();//添加退款记录
        if(!$refund_id){
            $Acta->rollback();
            $j=[
                'status'=>'error',
                'data'=>'系统错误,退货款订单生成错误'
            ];
            echo json_encode($j,JSON_UNESCAPED_UNICODE);
            exit;
        }

        //更改原订单退货款状态
        $ra=M('activity_apply')->where(['apply_id'=>$data['apply_id']])->data(['refund_id'=>$refund_id])->save();
        if(!$ra){
            $Acta->rollback();
            $j=[
                'status'=>'error',
                'data'=>'系统错误,更改退货款状态错误'
            ];
            echo json_encode($j,JSON_UNESCAPED_UNICODE);
            exit;
        }

        $Acta->commit();
        $j=[
            'status'=>'success',
            'data'=>'生成退款订单成功，等待申核中！'
        ];
        echo json_encode($j,JSON_UNESCAPED_UNICODE);
        exit;

        //成功提交退款申请


    }
}